﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlertSystem.Business
{
    public partial class Warning_Type
    {
        #region Singleton Pattern
        private static object _lock = new object();
        private static List<Warning_Type> _instance;
        public static List<Warning_Type> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.Warning_Types.ToList();
                        }
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.Warning_Types.ToList();
                }
            }
        }
        #endregion Singleton Pattern

        #region static method
        public static Warning_Type GetByID(string warning_type, string system_id)
        {
            try
            {
                return GetInstance().Find(p => p.WarningType == warning_type && p.System_Id == system_id);
            }
            catch
            {
                return null;
            }
        }

        public static Warning_Type FindItem(Predicate<Warning_Type> predicate)
        {
            try
            {
                return GetInstance().Find(predicate);
            }
            catch
            {
                return null;
            }
        }

        public static List<Warning_Type> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord, string warningType, string system_id, int? severity, string priority, string description)
        {
            var resutl = context.Warning_Types.AsQueryable();
            if (!string.IsNullOrWhiteSpace(warningType))
            {
                resutl = resutl.Where(p => p.WarningType.Contains(warningType));
            }
            if (!string.IsNullOrWhiteSpace(system_id))
            {
                resutl = resutl.Where(p => p.System_Id.Contains(system_id));
            }
            if (severity != null)
            {
                resutl = resutl.Where(p => p.Severity == severity);
            }
            if (!string.IsNullOrWhiteSpace(priority))
            {
                resutl = resutl.Where(p => p.Priority.Contains(priority));
            }
            if (!string.IsNullOrWhiteSpace(description))
            {
                resutl = resutl.Where(p => p.Description.Contains(description));
            }
            totalRecord = resutl.Count();
            return resutl.Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize).Take(pageSize).ToList();
        }

        public static IQueryable<Warning_Type_Custom> Warning_TypeCustomQuery(DBDataContext context)
        {
            return context.Warning_Types.Select(p => new Warning_Type_Custom()
                {
                    WarningType = p.WarningType,
                    System_Id = p.System_Id,
                    MappedField = p.WarningType + " | " + p.System_Id
                });
        }

        public static List<Warning_Type_Custom> GetWarning_TypeCustomSource()
        {
            using (var context = new DBDataContext(AppSetting.ConnectionString))
            {
                return Warning_TypeCustomQuery(context).ToList();
            }
        }
        #endregion static method
    }

    public class Warning_Type_Custom
    {
        public string WarningType { get; set; }
        public string System_Id { get; set; }
        public string MappedField { get; set; }
    }
}
